﻿/*-------------------------------------------------------------------------
* 命名空间: XrayDirection.DAL.LocalDataAccess/InputOrderAccess
* 类       名: InputOrderAccess
* 功      能:  TD
* 时      间:  2024/2/24 13:41:28
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using XrayDirection.Common;
using XrayDirection.DAL.DataEntity;
using XrayDirection.Extensions;

namespace XrayDirection.DAL.LocalDataAccess
{
    public class InputOrderAccess
    {
        /// <summary>
        /// 添加一条数据到AddInputOrders表中
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public static DataResult AddInputOrder(InputOrderEntity order)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    context.InputOrders.Add(order);
                    int affectRows = context.SaveChanges();
                    if (affectRows > 0)
                    {
                        result.State = true;
                    }
                    else
                    {
                        result.Message = ConstantManager.DatabaseAddError;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }


        public static DataResult<List<InputOrderEntity>> GetInputOrders()
        {
            DataResult<List<InputOrderEntity>> result = new DataResult<List<InputOrderEntity>>();
            try
            {
                using (var context = new DataContext())
                {
                    result.Data = context.InputOrders
                        .OrderBy(order => order.Id)
                        .ToList();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }


        public static DataResult DeleteOrderById(int id)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    var order = context.InputOrders.Find(id);
                    if (order != null)
                    {
                        context.InputOrders.Remove(order);
                        int affectRows = context.SaveChanges();
                        if (affectRows > 0)
                        {
                            result.State = true;
                        }
                        else
                        {
                            result.Message = "数据删除失败";
                        }
                    }
                    else
                    {
                        result.Message = "请选中要删除的工单!";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }
    }
}
